package com.sgb.goods.view.activity.vpm;

import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgb.goods.BR;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityOperationRecordBinding;
import com.sgb.goods.entity.MemberPriceEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.viewmodel.OperationRecordViewModel;
import com.sgb.goods.widget.pop.ShowVipPriceDetailWindow;

import java.util.LinkedHashMap;

@Route(path = Constants.ARouter.AR_OPERATION_RECORD)
public class OperationRecordActivity extends BaseActivity<GoodsActivityOperationRecordBinding, OperationRecordViewModel> {

    private ShowVipPriceDetailWindow mPopWindow;

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_operation_record;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(OperationRecordViewModel.class, BR.viewModel);
    }

    @Override
    protected void init() {
        mDataBinding.head.title.setText("操作记录");
        if (getIntent().getSerializableExtra("MemberPriceEntity") != null) {
            mViewModel.detail.postValue((MemberPriceEntity) getIntent().getSerializableExtra("MemberPriceEntity"));
        }
    }

    @Override
    protected void initObserve() {
        mViewModel.detail.observe(this, data -> {
            mViewModel.adapter.setNewData(data.itemMemberList);
            if(null == data) {
                mViewModel.showNoData(mDataBinding.recyclerview);
            }
        });

        mViewModel.detailSku.observe(this, memberPriceSkuDetail -> {
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            map.put("规格属性", memberPriceSkuDetail.get(0).skuDetails);
            if (memberPriceSkuDetail.get(0).parityPrice != null && (!TextUtils.isEmpty(memberPriceSkuDetail.get(0).promotionPrice)) && !memberPriceSkuDetail.get(0).promotionPrice.equals("0.00"))
                map.put("销售价/促销价", "¥" + memberPriceSkuDetail.get(0).parityPrice + "/¥" + memberPriceSkuDetail.get(0).promotionPrice);
            if (memberPriceSkuDetail.get(0).parityPrice != null && (TextUtils.isEmpty(memberPriceSkuDetail.get(0).promotionPrice) || memberPriceSkuDetail.get(0).promotionPrice.equals("0.00")))
                map.put("销售价", "¥" + memberPriceSkuDetail.get(0).parityPrice);
            if (memberPriceSkuDetail.get(0).parityPrice == null && memberPriceSkuDetail.get(0).promotionPrice != null)
                map.put("促销价", "¥" + memberPriceSkuDetail.get(0).promotionPrice);
            for (int i = 0; i < memberPriceSkuDetail.size(); i++) {
                switch (memberPriceSkuDetail.get(i).level) {
                    case 0:
                        map.put("普通会员", "¥" + memberPriceSkuDetail.get(i).price);
                        break;
                    case 1:
                        map.put("一星会员", "¥" + memberPriceSkuDetail.get(i).price);
                        break;
                    case 2:
                        map.put("二星会员", "¥" + memberPriceSkuDetail.get(i).price);
                        break;
                    case 3:
                        map.put("三星会员", "¥" + memberPriceSkuDetail.get(i).price);
                        break;
                    case 4:
                        map.put("四星会员", "¥" + memberPriceSkuDetail.get(i).price);
                        break;
                    case 5:
                        map.put("五星会员", "¥" + memberPriceSkuDetail.get(i).price);
                        break;
                }
            }
            map.put("发布人", TextUtils.isEmpty(mViewModel.mData.supplierContact) ? "/" : mViewModel.mData.supplierContact);
            map.put("联系方式", TextUtils.isEmpty(mViewModel.mData.supplierPhone) ? "/" : mViewModel.mData.supplierPhone);
            showPopWindow(map);
        });
    }

    @Override
    protected void initListener() {
        mDataBinding.head.ivBack.setOnClickListener(v -> finish());
        mViewModel.adapter.setItemChildClickListener((adapter, view, position) -> {
            mViewModel.mData = mViewModel.adapter.getItem(position);
            mViewModel.getSkuMemberPriceDetail();
        });
    }

    private void showPopWindow(LinkedHashMap<String, String> map) {
        if (mPopWindow == null) {
            mPopWindow = new ShowVipPriceDetailWindow(this);
            mPopWindow.setBgView(mDataBinding.grayLayout);
            mPopWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
            mPopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        }
        mPopWindow.setOnResultClick((key1, key2, key3) -> mPopWindow.dismiss());
        mPopWindow.updateData(map, "会员价详情", "关闭");
        mPopWindow.showDownPopwindow(mDataBinding.grayLayout, true);
    }
}
